package com.himiotech.core.framework.config.mvc;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.himiotech.core.framework.config.HimioConfig;
import com.himiotech.core.framework.config.interceptor.AbstractRepeatSubmitInterceptor;
import com.himiotech.core.framework.config.interceptor.DemoInterceptor;
import com.himiotech.core.framework.helper.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;

/**
 * mvc全局配置
 *
 * @author wuwenbin
 **/
@Configuration
public class HimioWebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private AbstractRepeatSubmitInterceptor abstractRepeatSubmitInterceptor;
    @Autowired
    private DemoInterceptor demoInterceptor;
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private ShiroDialect shiroDialect;


    /**
     * 配置html页面映射路径
     *
     * @return
     */
    @Bean
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setApplicationContext(applicationContext);
        String prefix = HimioConfig.getWebappPathStatic() + "/";
        templateResolver.setPrefix("file:///" + prefix);
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setForceTemplateMode(true);
        templateResolver.setCheckExistence(true);
        templateResolver.setCacheable(false);
        return templateResolver;
    }

    /**
     * 模板引擎自定义配置
     *
     * @return
     */
    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        templateEngine.setEnableSpringELCompiler(true);
        //加入shiro的断言，用于前端的thymeleaf的限制
        templateEngine.addDialect(shiroDialect);
        return templateEngine;
    }

    /**
     * viewResolver 自定义配置
     *
     * @return
     */
    @Bean
    public ThymeleafViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        return viewResolver;
    }

    /**
     * 添加一些虚拟路径的映射
     * 上传文件的路径
     * 如果配置了七牛云上传，则上传路径无效
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //静态资源文件夹路径
        String staticPath = HimioConfig.getStaticPathStatic();
        registry.addResourceHandler("/static/**").addResourceLocations("file:///" + staticPath + "/");
        //上传文件存放的文件夹路径
        String uploadPath = HimioConfig.getUploadPath();
        registry.addResourceHandler(Constants.UPLOAD_FILE_PREFIX + "/**").addResourceLocations("file:///" + uploadPath);
    }


    /**
     * 自定义拦截规则
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(demoInterceptor).addPathPatterns(HimioConfig.getDemoIncludeUrls()).excludePathPatterns(HimioConfig.getDemoExcludeUrls());
        registry.addInterceptor(abstractRepeatSubmitInterceptor).addPathPatterns("/**");
    }
}